package com.zhida.jiyangcenter.mian.fragment

import android.os.Bundle
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chooongg.base.annotation.ContentLayout
import chooongg.base.utils.getStatusBarHeight
import chooongg.base.utils.logE
import chooongg.base.utils.startActivity
import chooongg.base.utils.tipFail
import chooongg.base.widget.recyclerView.divider.HorizontalDividerItemDecoration
import chooongg.http.APIException
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.WebActivity
import com.zhida.jiyangcenter.WebNoTokenActivity
import com.zhida.jiyangcenter.account.LoginPassActivity
import com.zhida.jiyangcenter.api.APIFamous
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.FamousListEntity
import com.zhida.jiyangcenter.mian.AssessmentActivity
import com.zhida.jiyangcenter.mian.BaseFragment
import com.zhida.jiyangcenter.mian.MainHomeLeftActivity
import com.zhida.jiyangcenter.mian.MainHomeRightActivity
import com.zhida.jiyangcenter.modle.*
import com.zhida.jiyangcenter.utils.getLocation
import com.zhida.jiyangcenter.utils.glideImage
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.fragment_main_home.*


@ContentLayout(R.layout.fragment_main_home)
class MainHomeFragment : BaseFragment() {

    //声明AMapLocationClient类对象
    private lateinit var locationClient: AMapLocationClient


    override fun getTitle(): String {
        return "首页"
    }

    val adapter = Adapter()
    override fun initConfig(savedInstanceState: Bundle?) {
        super.initConfig(savedInstanceState)

        view_status_bar.updateLayoutParams<ConstraintLayout.LayoutParams> {
            height = getStatusBarHeight()
        }
        rv_home.layoutManager = object : LinearLayoutManager(context) {
            override fun canScrollVertically(): Boolean {
                return false
            }
        }
        rv_home.addItemDecoration(
            HorizontalDividerItemDecoration.Builder(requireContext())
                .sizeResId(R.dimen.marginContentSmall).colorResId(R.color.colorBack).build()
        )

        rv_home.adapter = adapter
        adapter.setOnItemClickListener { _, view, position ->
            startActivity<HealthyProgrammeActivity> {
                putExtra("title", adapter.data[position].title)
                putExtra("uuid", adapter.data[position].id)
            }
        }
        iv_home_archives.setOnClickListener {
            startActivity<MedicalListActivity> { }
        }
        iv_home_assessment.setOnClickListener {
            startActivity<AssessmentActivity> { }
        }
        view_home1.setOnClickListener {
            startActivity<MainHomeRightActivity> { }
        }
        view_home2.setOnClickListener {
            startActivity<MainHomeLeftActivity> { }
        }
        iv_home_tijian.setOnClickListener {
            startActivity<HealthyTestActivity> { }
        }
        iv_home_jiance.setOnClickListener {
            startActivity<EquipmentListActivity> { }
        }
        iv_home_examination.setOnClickListener {
            startActivity<WebActivity> {
                putExtra(
                    WebActivity.URL,
                    "http://beauty.hebzd.com/media/healthDocument.html"
                )
                putExtra(WebActivity.TITLE, "电子健康档案")
            }
        }
        locationClient = getLocation(requireContext()) {
            if (it.errorCode == AMapLocation.LOCATION_SUCCESS) {// 定位成功
                tv_fragment_main_address.text = it.city

            } else {// 定位失败
                logE("${it.errorCode}定位失败：${it.errorInfo ?: "未知原因"}")
            }
        }

        iv_home_consultation.setOnClickListener {
            startActivity<WebNoTokenActivity> {
                putExtra(WebNoTokenActivity.URL, "http://s.appurls.cn/s/332345kpo")
                putExtra(WebNoTokenActivity.TITLE, "健康问诊")
            }
        }
        iv_home_peisong.setOnClickListener {
            startActivity<WebNoTokenActivity> {
                putExtra(WebNoTokenActivity.URL, "https://m.ddky.com/")
                putExtra(WebNoTokenActivity.TITLE, "药品配送")
            }
        }
        iv_home_fuwu.setOnClickListener {
            startActivity<WebNoTokenActivity> {
                putExtra(WebNoTokenActivity.URL, "http://h5.yihu365.com/")
                putExtra(WebNoTokenActivity.TITLE, "医护服务")
            }
        }
        bga_fragment_main.setAdapter { _, itemView, model, position ->
            glideImage(itemView as ImageView, model.toString())
        }
        bga_fragment_main.setData(ArrayList<String>().apply {
            add("http://beauty.hebzd.com/media/img/banner0.png")
            add("http://beauty.hebzd.com/media/img/banner1.png")
            add("http://beauty.hebzd.com/media/img/banner2.png")
            add("http://beauty.hebzd.com/media/img/banner3.png.png")
        }, null)
        getData()
    }

    private fun getData() {
        lifecycleScope.request(
            { APIFamous.service().list() },
            object : BaseAPI<List<FamousListEntity>>() {
                override fun onSuccess(t: List<FamousListEntity>?, state: Int, msg: String) {
                    adapter.setNewData(t)
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                }

            })
    }

    class Adapter :
        BaseQuickAdapter<FamousListEntity, BaseViewHolder>(R.layout.item_fragment_home) {
        override fun convert(helper: BaseViewHolder, item: FamousListEntity?) {
            glideImage(helper.getView(R.id.iv_item_home), item?.img ?: "")
            helper.setText(R.id.tv_item_home_title, item?.title)
                .setText(R.id.tv_item_home_context, item?.title_s)
                .setText(R.id.tv_item_home_money, item?.volume.toString() + "*")
        }

    }

    override fun onResume() {
        super.onResume()
        locationClient.startLocation()

    }

    override fun onPause() {
        locationClient.stopLocation()
        super.onPause()
    }

    override fun onDestroy() {
        locationClient.onDestroy()
        super.onDestroy()
    }
}